package com.tecnoin.tintconta.core.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.PrintWriter;

/**
 * Servlet implementation class TECNOINAjaxPortlet
 */
@WebServlet(name = "TECNOINAjaxServlet", urlPatterns = { "/TECNOINAjaxServlet" })
public class TECNOINAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TECNOINAjaxServlet() 
    {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
	{
		String operacion = request.getParameter(ConstantesWebTINTConta.AJAX_PARAM_NAME_REQUEST_OPERATION);
		TECNOINAjaxOperationHandler oph = loadOperationInstance(operacion);
		oph.setRequest(request);
		oph.setResponse(response);
		oph.setSession(request.getSession());
		oph.executeAjax();
	}

	private TECNOINAjaxOperationHandler loadOperationInstance(String operacion) 
	{
		Class cl = null;
		TECNOINAjaxOperationHandler h = null;
		try 
		{
			cl = Class.forName(operacion);
			try {
				h = (TECNOINAjaxOperationHandler) cl.newInstance();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		}
		
		return h;
	}

}
